<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 技术支持管理
 * @package app\Technology
 * @author LJQ<245369209@qq.com>
 */
class Technology extends Api

{
	protected $wfType = 7;
    /**
   *  技术支持列表
   * @param string 
   * @return Json
   */

	public function technologyList(){
             $where['categoryId'] = ['eq',3] ;
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 技术支持列表
			$info =collection(model('Technology')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//技术支持列表
			$info =collection(model('Technology')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Technology')->where($where)->count('id'); 
		
    	if ($info && $count == 0) {
         	return json(['code'=>1,'msg'=>'没有找到技术支持','count'=>$count,'data'=>$info]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'保障技术支持','count'=>$count,'data'=>$info]);
       	}
        return json(['code'=>0,'msg'=>'数据异常']);
   	 }
   	  
   	      //添加技术支持
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Basic/Technology');
          $result = $model->change($data);
          if (false != $result) {
             setlog('Technologyadd','Technology',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'技术支持添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'技术支持添加失败,请检查']);
          }
      }

   	

   /**
   * 修改技术支持
   * @param  string TechnologyId 技术支持id,
   * @return Json
   */
   	public function edit($data,$technologyId){
        
          if (!$technologyId) {
              return json(['code'=>0,'msg'=>'技术支持不存在,请检查']);
          }
          $model = model('Basic/Technology');     
          if ($model->change($data)) {
              setlog('Technologyupdate','Technology',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'技术支持信息修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'技术支持信息修改失败,请检查']);
            }
    }
    /**
   *  删除技术支持
   * @param string TechnologyId 技术支持id,
   * @param Json
   */
     
    public function del($technologyId) {
        $model = model('Basic/Technology');
      	if ( !$technologyId ||empty(($model->where(array('id'=>$technologyId))->find()))) {
           	return  json(['code'=>0,'msg'=>'技术支持id不存在,请检查']);
        }
        if($model->destroy(['id'=>$technologyId]) !== FALSE){
           setlog('Technologydel','Technology',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'技术支持删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'技术支持删除失败,请检查']);
       }
    }

     
   /**
   *   查询技术支持
   * @param keyword技术支持名称
   * @return Json
   */
   	public function search($keyword=null){
   		$where['categoryId'] = ['eq',3] ;

		if($keyword){
			
			$where['fullName'] = ['like','%'.$keyword.'%'];
		}
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//技术支持列表
			$info =collection(model('Technology')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//技术支持列表
			$info =collection(model('Technology')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Technology')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到技术支持','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'技术支持','count'=>$count,'data'=>$info]);
       	}
      }



	/**
	 * 获取技术支持位置
	 * @param string $enterpriseId
	 * @return Json
	 */
	public function getTechnologyPosition($technologyId){
		$position = model('technology')->where('id',$technologyId)->value('latitudeLongitude');
		if($position){
			return json(['code'=>1,'msg'=>'','data'=>$position]);
		}else{
			return json(['code'=>0,'msg'=>'没有对应位置']);
		}
	}
      
      /**
       * 申请审核
       * @param string $rescueId
       */
      public function audit($technologyId = null){
      	$workflow = model('Workflow')->where('wfType',$this->wfType)->find();
      	if(empty($workflow)||empty($workflow->steps)){
      		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
      	}else{
	      	$technology = model('Technology')->get($technologyId);
	      	if($technology->isUpdate(true)->save(['checkStatus'=>2]) !== false){
	      		$data['title'] = $technology['fullName'];
	      		$data['wfFlowInfoId'] = $workflow['id'];
	      		$data['wfType'] = $this->wfType;
	      		$data['busiId'] = $technologyId;
	      		$data['applyPerson'] = session('user_auth.id');
	      		if(model('Wfitem')->save($data)){
	      			return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
	      		}else{
	      			return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
	      		}
	      	}
      	}
      	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      
      }
}